package cn.com.bluemoon.daps.system.service.impl;

import cn.com.bluemoon.daps.common.domain.UserInfoHolder;
import cn.com.bluemoon.daps.common.exception.DapException;
import cn.com.bluemoon.daps.domp.api.LocalUserService;
import cn.com.bluemoon.daps.domp.api.UserService;
import cn.com.bluemoon.daps.system.entity.DapSystemLogin;
import cn.com.bluemoon.daps.system.mapper.DapSystemLoginMapper;
import cn.com.bluemoon.daps.system.service.DapSystemLoginService;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collector;
import java.util.stream.Collectors;

/**
 * <p>
 * 数据资产登录用户表 服务实现类
 * </p>
 *
 * @author admin
 * @since 2020-11-26
 */
@Slf4j
@Service
@Transactional(rollbackFor = {DapException.class, Exception.class})
public class DapSystemLoginServiceImpl extends ServiceImpl<DapSystemLoginMapper, DapSystemLogin>
        implements DapSystemLoginService {
    @Resource
    private LocalUserService userService;

    @Override
    public void upsertByUserInfoHolder() {
        UserInfoHolder.getUserInfo().ifPresent(userInfo -> {
            DapSystemLogin systemLogin = new DapSystemLogin();
            systemLogin.setToken(userInfo.getToken());
            systemLogin.setUserName(userInfo.getUserName());
            systemLogin.setMobile(userInfo.getMobile());
            systemLogin.setAccount(userInfo.getAccount());
            upsert(systemLogin);
        });
    }

    private void upsert(DapSystemLogin systemLogin) {
        Wrapper<DapSystemLogin> wrapper = bmLambdaQuery().eq(DapSystemLogin::getAccount, systemLogin.getAccount()).last(" limit 1");
        DapSystemLogin one = getOne(wrapper);
        if (one != null) {
            update(systemLogin, wrapper);
        } else {
            save(systemLogin);
        }
        log.info("记录用户信息");
    }

    @Override
    public void upsert(String username, String password, String token) {
        if (StringUtils.isNotBlank(token)) {
            log.info("用户{}登录成功，保存登录信息", username);
            DapSystemLogin systemLogin = new DapSystemLogin();
            String digest = DigestUtils.md5DigestAsHex(password.getBytes(StandardCharsets.UTF_8));
            systemLogin.setPassword(digest);
            systemLogin.setToken(token);
            UserInfoHolder.UserInfo userInfo = userService.getUserInfoByUserId(StrUtil.trim(username));
            if (userInfo != null) {
                systemLogin.setUserName(userInfo.getUserName());
                systemLogin.setMobile(userInfo.getMobile());
                systemLogin.setAccount(userInfo.getAccount());
                systemLogin.setEmail(userInfo.getEmail());
            }
            upsert(systemLogin);
        } else {
            log.error("登录失败不进行保存用户{}信息", username);
        }
    }

    @Override
    public List<DapSystemLogin> listUserByAccount(List<String> accounts) {
        if (accounts != null && !accounts.isEmpty()){
            List<DapSystemLogin> res = list(bmLambdaQuery().in(DapSystemLogin::getAccount, accounts));
            return res;
        }
        return Collections.emptyList();
    }


}
